<?php

namespace App\Http\Controllers\Establish;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Establish;
use App\Models\Type;
use App\Models\Ctype;
use Illuminate\Support\Facades\Redis;
use Illuminate\Pagination\LengthAwarePaginator;

class EstablishController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    protected $ret;

    public function __construct()
    {
        $this->ret = [];
    }

    public function index()
    {
         $data = Establish::get();
         //dd($data);
         return view("establish.index",['list'=>$data[0]]);
    }



    public function add(Request $request)
    {
        $data = $request->input();
        $id = $data['id'];
        unset($data['_token']);
        array_shift($data);

        //dd($data);
        $re = Establish::where("id",$id)->update($data);
        //dd($re);
        if($re){
            return redirect("establish/index");
        }
    }

    //20181120 网站更新设置
    public function updateSite(Request $request)
    {
        $info = $request->input("info");
        $info = $info ? $info : "index";
        $Type = new Type();
        if($info =="article"){
             $data = $Type->getList("ARTICLE");  //获取文章类别 article
         }else{
             $data = Ctype::where("pid",0)->whereNull("deleted_at")->where("state",1)->select("id","title","state")->get(); //course
             foreach($data as $k=>$v){
                $id = $v->id;
                $v->two = Ctype::where("pid",$id)->whereNull("deleted_at")->where("state",1)->select("id","title","state")->get(); //course
             }
             
         }
       
        //dd($data);
        $this->ret['data'] = $data;
        $this->ret['info'] = $info;
        return view("establish.updatesite", $this->ret);
    }

    //20181120 ajax更新redis缓存
    public function  updateCache(Request $request){
        $info = $request->input("info","0");
        $list = $request->input("list");
        if($info=='index'){
            redis::del("index_banner");     //banner
            redis::del("index_selected");   //精选文章
            redis::del("index_free");       //免费课程
            redis::del("index_hot");        //推荐课程

        }elseif($list == 'course'){
            Redis::set("cache_course_type_".$info, 1);
        }else{
            Redis::set("cache_article_type_".$info, 1);
        }

        return json_encode(array("code"=>1, "msg"=>"操作成功"));
    }

    public function  showCache(){
        if(Redis::exists("cache_article_type_0") && Redis::get("cache_article_type_0") != ''){
            $article_type_id = Redis::get("cache_article_type_0");
            echo $article_type_id;
        }else{
            echo 222;
        }
    }

    /**
      * 查看所有类型标签
     */
    public function  showAllKey(){
        if(Redis::exists("all_type_arr") && Redis::get("all_type_arr") != '' ){
            $new = Redis::get("all_type_arr");
            $count = Redis::get("all_type_num");
            $type = json_decode($new,true);
        }else{
            $all = Redis::keys("*");
            $arr = [];
            foreach($all as $v){
                $allstr = preg_replace('/[0-9]*/','',$v);
                $arr[] = $allstr;
            }

            $type = array_unique($arr);//获取所有种类
            $count = count($all);
            Redis::set("all_type_arr", json_encode($type));
            Redis::set("all_type_num", $count);
        }

        $this->ret['type'] = $type;
        $this->ret['count'] = $count;

        return view("establish.showAllkey",$this->ret);


    }
    /**
     * 更新标签
     */

    public function updateType(){
        $all = Redis::keys("*");
        $arr = [];
        foreach($all as $v){
            $allstr = preg_replace('/[0-9]*/','',$v);
            $arr[] = $allstr;
        }

        $type = array_unique($arr);//获取所有种类
        $count = count($all);
        Redis::set("all_type_arr", json_encode($type));
        Redis::set("all_type_num", $count);
        return redirect("/establish/showAllKey");
    }



}
